package dao;

import java.sql.SQLException;
import java.util.ArrayList;

import pojo.ItemAttr;
import pojo.UserAttr;

public class UserDao extends DbDao{
    /**
     * 对没有参数的查询的重载 
     */
    public boolean select() {
        String sql  = String.format("SELECT * FROM `%s`", UserAttr.TableName);
        return this.query(sql);
    }
    
    /**
     * 得到一行记录 
     */
    public UserAttr fetch() {
        try {
            while(this._resultSet.next()) {
                return this.genUserAttr();
            }
            return null;
        } catch(Exception ex) {
            return null;
        }
    }
   
    /**
     * 得到所有的查询结果集  
     */
    public ArrayList<UserAttr> fetchAll() {
        try {
            ArrayList<UserAttr> userList  = new ArrayList<UserAttr>();
            while(this._resultSet.next()) {
                userList.add(this.genUserAttr()); 
            }
            return userList;
        } catch(Exception ex) {
            System.out.println("Error: " + ex.getMessage());
            return null;
        }
    }
    
    /**
     * 添加用户的重载
     * 
     * @param UserAttr userAttr 用户属性对象
     * @return boolean true or false 是否添加成功
     */
    public boolean insert(UserAttr userAttr) {
        try {
            String sql  = String.format("INSERT INTO `%s` " +
                                		"(`user_id`, `user_name`, `user_passwd`," +
                                		"`user_type`, `user_email`, `create_time`)" +
                                		" VALUES " +
                                		"(NULL, '%s', '%s', '%d', '%s', '%s')",
                                		UserAttr.TableName,
                                		userAttr.getUserName(),
                                		userAttr.getUserPasswd(),
                                		userAttr.getUserType(),
                                		userAttr.getUserEmail(),
                                		userAttr.getCreateTime());
            return this._db.insert(sql);
        } catch(Exception ex) {
            return false;
        }
    }
   
    /**
     * 更新数据的重载，参数为用户的属性对象 
     * 
     * @param userAttr 用户属性对象
     * @return true or false 是否更新成功
     */
    public boolean update(UserAttr userAttr) {
        try {
            String sql  = String.format("UPDATE `%s` SET " +
                                		"`user_name` = %s," +
                                		"`user_passwd` = '%s'," +
                                		"`user_type` = %d" +
                                		"`user_email` = '%s'" +
                                		"`create_time` = '%s'" +
                                		" WHERE " +
                                		"`user_id` = %d",
                                		UserAttr.TableName,
                                        userAttr.getUserName(),
                                        userAttr.getUserPasswd(),
                                        userAttr.getUserType(),
                                        userAttr.getUserEmail(),
                                        userAttr.getCreateTime(),
                                        userAttr.getUserId());
            return this._db.update(sql);
        } catch(Exception ex) {
            return false;
        }
    }
   
    /**
     * 删除数据重载
     * 
     * @param int userId 用户ID
     * @return boolean 是否成功
     */
    public boolean delete(int userId) {
        String sql  = String.format("DELETE FROM `%s` WHERE `user_id` = %d",
                                    UserAttr.TableName,
                                    userId);
        return this._db.delete(sql);
    }
    
    /**
     * 得到用户信息对象
     */
    private UserAttr genUserAttr() throws SQLException {
        UserAttr userAttr  = new UserAttr();
        userAttr.setUserId(this._resultSet.getInt("user_id"));
        userAttr.setUserName(this._resultSet.getString("user_name"));
        userAttr.setUserPasswd(this._resultSet.getString("user_passwd"));
        userAttr.setUserType(this._resultSet.getInt("user_type"));
        userAttr.setUserEmail(this._resultSet.getString("user_email"));
        userAttr.setCreateTime(this._resultSet.getTimestamp("create_timE").toString());
        
        return userAttr;
    }
    
    /**
     * 重载一下得到最后一条记录的ID 
     */
    public int getLastInsertId() {
        return this.getLastInsertId(UserAttr.TableName, "user_id");
    }
   
    /**
     * 看用户名是否已经使用 
     * 
     * @param String userName 用户名
     * @return boolean true or false 是否存在
     */
    public boolean isUser(String userName) {
        String sql  = String.format("SELECT " +
                            		"*" +
                            		" FROM " +
                            		"`%s`" +
                            		" WHERE " +
                            		"`user_name` = '%s';",
                                    UserAttr.TableName,
                                    userName);
        if(false == this.query(sql)) {
            return false;
        }
        if(null == this.fetch()) {
            return false;
        }
        
        return true;
    }
   
    /**
     * 用户登陆 
     */
    public UserAttr userLogin(String userName, String userPasswd) {
       String sql   = String.format("SELECT * FROM `pollsys_users` " +
                               		"WHERE `user_name` = '%s' AND `user_passwd` = '%s'",
                                    userName, userPasswd);
       System.out.println(sql);
       if(false == this.query(sql)) {
           return null;
       }
       return this.fetch();
    }
    
}
